package edu.avans.hartigehap.service.jpa;

import java.math.BigDecimal;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import edu.avans.hartigehap.domain.StateException;
import edu.avans.hartigehap.domain.WebDiscountInterface;
import edu.avans.hartigehap.domain.WebDividebyDiscount;
import edu.avans.hartigehap.domain.WebMinusDiscount;
import edu.avans.hartigehap.repository.WebDiscountRepository;
import edu.avans.hartigehap.service.WebDiscountService;

@Service("webDiscountService")
@Repository
@Transactional(rollbackFor = StateException.class)
public class WebDiscountServiceImpl implements WebDiscountService{

	@Autowired
	private WebDiscountRepository webDiscountRepository;

	@Transactional
	public WebDiscountInterface createNewWebDiscount() {
		Random random = new Random();
		int i = random.nextInt(2);
		int discount = random.nextInt(5) + 1;

		if(i == 1) {
			BigDecimal bd = new BigDecimal(discount);
			WebDiscountInterface webDiscount = new WebDividebyDiscount(bd);
			webDiscountRepository.save(webDiscount);
			return webDiscount;
		} else if (i == 0) {
			BigDecimal bd = new BigDecimal(discount);
			WebDiscountInterface webDiscount = new WebMinusDiscount(bd);
			webDiscountRepository.save(webDiscount);
			return webDiscount;
		}
		return null;
	}

}
